﻿#if Yitongda

using UnityEngine;
using System;


public class YitongdaAdController : MonoBehaviour,AdController
{
	public INativeAdManager nativeAdManager { get; set; }

	public IAdSdKCallback adSdKCallback { get; set; }

	public bool IsVideoReady => nativeAdManager.isVideoReady();
	public bool IsInterstitialReady => nativeAdManager.isInterstitialReady();
	
	private bool m_DisableInterAndBanner;
	
	private void Awake()
	{
		DontDestroyOnLoad(gameObject);
	}

	public void Init()
	{
#if IAP
        m_DisableInterAndBanner = VoodooPremium.IsPremium();
#endif
#if UNITY_ANDROID
		nativeAdManager = new AndroidNativeAdManager();
#elif UNITY_IOS

#endif
		adSdKCallback = new GameObject("AdSDKCallback").AddComponent<AdSDKCallback>();

		adSdKCallback.onInitSuc = OnInitSuc;
		adSdKCallback.onInitFail = OnInitFail;
		
		Invoke(nameof(OnInitSuc),2);
	}

	void OnInitSuc()
	{
		Debug.Log($"yitongda init suc");
		
		ShowBanner();
		
		DoActionDelay.DelayAction(1,LoadInterstitialAd);
		DoActionDelay.DelayAction(1.5f,LoadVideoAd);
		
	}
	void OnInitFail(string err)
	{
		Debug.LogError($"yitongda init fail:{err}");
	}
	
#region  banner

	public void LoadBanner()
	{
	    //nativeAdManager.LoadBanner();
	}

	public void DisableBannerAndInterstitial()
	{
		HideBanner();
	}

	public void EnableBannerAndInterstitial()
	{
		ShowBanner();
	}
	public void ShowBanner()
	{
		if (m_DisableInterAndBanner) return;
		
		Debug.Log($"yitongda  -> ShowBanner");
		
		adSdKCallback.onBannerShowSuc = HandleBannerSuc;
		adSdKCallback.onBannerShowFail = HandleBannerFail;
	    nativeAdManager.ShowBanner();
	    
	    //AdHelper.showBanner(new UnityBannerListener(this), 16, 16, 8,
		 //   AdHelper.Horizon.HORIZ_MODEL_CENTER);
    }

	private void HandleBannerSuc()
	{
		Debug.Log("yitongda -> HandleBannerSuc");
	}

	private void HandleBannerFail(string err)
	{
		Debug.LogError("HandleBannerFail:"+err);
		
		Invoke(nameof(ShowBanner),10);
	}
	public void HideBanner()
	{
		nativeAdManager.CloseBanner();
	}
#endregion

    
    #region FullScreen

    private bool m_InterstitialSuc = false;
    
    public void LoadInterstitialAd()
    {
		nativeAdManager.LoadFullScreenAd();
    }

    public void ShowInterstitialAd()
    {
	    if (m_DisableInterAndBanner) return;
	    
	    Debug.Log($"yitongda  -> ShowInterstitialAd");
	    
	    m_InterstitialSuc = false;
	    this.adSdKCallback.onFullScreenShowSuc = () =>
	    {
		    Debug.Log($"yitongda interstitial suc");
		    m_InterstitialSuc = true;
	    };
	    this.adSdKCallback.onFullScreenShowFail = (err) =>
	    {
		    Debug.LogError($"yitongda interstitial err:{err}");
		    this.onInterstitialAdFail?.Invoke(err);
	    };
	    adSdKCallback.onFullScreenClose = () =>
	    {
		    Debug.Log($"yitongda interstitial Close:{m_InterstitialSuc}");
		    
		    if (m_InterstitialSuc)
		    {
			    this.onInterstitialAdSuc?.Invoke("");
		    }
		    else
		    {
			    this.onInterstitialAdClosed?.Invoke("");
		    }
	    };
	    nativeAdManager.ShowFullScreenAd();
    }

    #endregion

    #region video


    public void LoadVideoAd()
    {
	    nativeAdManager.LoadRewardVideoAd();
    }

    private bool m_PlayVideoSuc;
    public void PlayVideoAd()
    {
	    Debug.Log($"yitongda  -> PlayVideoAd");
	    
	    m_PlayVideoSuc = false;
	    
	    this.adSdKCallback.onVideoSuc =()=>
	    {
		    m_PlayVideoSuc = true;
		    
		    Debug.Log($"yitongda video suc:");
	    };
	    this.adSdKCallback.onVideoFail = (err)=>
	    {
		    GameDebug.LogError($"yitongda video error:{err}");
		    
		    onVideoAdFailedToShow?.Invoke(err);
	    };
	    this.adSdKCallback.onVideoClose = ()=>
	    {
		    Debug.Log($"yitongda video Close:{m_PlayVideoSuc}");
		    
		    if (!m_PlayVideoSuc)
		    {
			    onVideoAdClosed?.Invoke();
		    }
		    else
		    {
			    Debug.Log($"已播放成功，忽略关闭事件");
			    this.onVideoPlaySuc?.Invoke("");
		    }
	    };
	    
	    nativeAdManager.ShowRewardVideoAd();
    }

    #endregion
    
    
    public void Release()
    {
		
    }

    public void ReleaseBannerEvent()
    {
		
    }

    public void ReleaseInterstitialEvent()
    {
		
    }

    public void ReleaseVideoEvent()
    {
		
    }
    public Action<string> onBannerAdLoaded { get; set; }
    public Action<string> onBannerAdFailedToLoad { get; set; }
    public Action<string> onBannerShowSuc { get; set; }
    public Action<string> onBannerShowFailed { get; set; }
    public Action<string> onBannerAdOpening { get; set; }
    public Action<string> onBannerAdClosed { get; set; }
    public Action<string> onBannerAdLeavingApplication { get; set; }
    public Action<string> onInterstitialAdLoaded { get; set; }
    public Action<string> onInterstitialAdFailedToLoad { get; set; }
    public Action<string> onInterstitialAdSuc { get; set; }
    public Action<string> onInterstitialAdFail { get; set; }
    public Action<string> onInterstitialAdOpening { get; set; }
    public Action<string> onInterstitialAdClosed { get; set; }
    public Action<string> onInterstitialAdLeavingApplication { get; set; }
    public Action<string> onVideoAdLoaded { get; set; }
    public Action<string> onVideoAdFailedToLoad { get; set; }
    public Action onVideoAdOpening { get; set; }
    public Action<string> onVideoAdFailedToShow { get; set; }
    public Action<string> onVideoPlaySuc { get; set; }
    public Action onVideoAdClosed { get; set; }
}

public class UnityBannerListener : BannerListener
{
    private YitongdaAdController m_AdController;
    public UnityBannerListener(YitongdaAdController adController)
    {
        m_AdController = adController;
    }
    
    public void onAdFailedToLoad(REASON reason, string message)
    {
        //m_AdController.HandleOnBannerAdFailedToLoad(reason+", Msg:"+message);
    }

    public void onAdLoaded()
    {
        //m_AdController.HandleOnBannerAdLoaded();
    }

    public void onAdClosed()
    {
        
    }

    public void onAdOpened()
    {
       // m_AdController.HandleOnBannerAdOpened();
    }

    public void onAdImpression()
    {
        
    }
}

#endif
